import hooks = require('./user.hooks')
import filters = require('./user.filters')
import Rx = require('rxjs')
const sequelizeService = require('feathers-sequelize')

// Service method	HTTP method	Path
// .find()	GET	/users
// .get()	GET	/users/1
// .create()	POST	/users
// .update()	PUT	/users/1
// .patch()	PATCH	/users/1
// .remove()	DELETE	/users/1

class User {
  id = 0
  name = ''
  email = ''
  password = ''
}

class UserService {
  private users: User[] = []

  setup(app: any, path: string) {
    console.log('setup service: /' + path)
  }

  //GET
  find(params: any, callback: Function) {
    return Promise.resolve(this.users)
  }

  //GET
  get(id: string, params: any) {
    let result = this.users.find(item => item.name === id) || {}
    return Promise.resolve(result)
  }

  //POST
  create(data: User, params: any) {
    let user = new User()
    user.id = this.users.length + 1
    this.users.push({ ...user, ...data })

    return Promise.resolve(data)
  }

  update(id: string, data: User, params: any) {

    return Promise.resolve(data)
  }

  patch(id: string, data: User, params: any) {

    return Promise.resolve(data)
  }

  remove(id: string, params: any) {
    return Promise.resolve({
      id,
      success: true
    })
  }
}

export = function () {
  this.use('/user', new UserService())

  // Get our initialized service so that we can register hooks and filters
  const service = this.service('/user')
  service.hooks(hooks)
  if (service.filter) {
    service.filter(filters)
  }

  service.on('created', (message: any) => console.log('event created', JSON.stringify(message)))
  // service.on('removed', (message: any) => console.log('event removed', JSON.stringify(message)))
  // service.on('customEvent', (data: any) => console.log('event customEvent', JSON.stringify(data)))

  // service.emit('customEvent', {
  //   type: 'customEvent',·
  //   data: 'can be anything'
  // })

  let loginUser = [
    {
      email: 'lyaotian@qq.com',
      password: '123456'
    },
    {
      email: 'a@qq.com',
      password: '123456'
    },
  ]
  
  Rx.Observable.from(loginUser).subscribe(item => service.create(item))
}